package com.ipc.app.dal.dao.impl;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.ipc.app.dal.dao.UserDAO;
import com.ipc.app.dal.doObject.UserDO;

/**
 * @author 雪
 * 
 */
public class UserDAOImpl extends HibernateDaoSupport implements UserDAO {

	public String createUser(final UserDO userDO) {
		Serializable userID = this.getHibernateTemplate().save(userDO);
		return userID.toString();
	}

	public void updateUser(UserDO userDO) {
		this.getHibernateTemplate().update(userDO);
	}

	public UserDO getUserByID(String userID) {
		UserDO userDO = (UserDO) this.getHibernateTemplate().get(UserDO.class,
				userID);
		return userDO;
	}

	@SuppressWarnings("unchecked")
	public UserDO getUserByMobile(String mobile) {
		List<UserDO> userDO = this.getHibernateTemplate().findByNamedQuery(
				"queryUserByMobile", mobile);
		if (null == userDO || userDO.size() == 0)
			return null;
		return userDO.get(0);
	}

	public void updateLoginTime(final String userID) {
		this.getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.getNamedQuery("updateLastLoginTime");
				query.setParameter("TIME", Calendar.getInstance().getTime());
				query.setParameter("USERID", userID);
				query.executeUpdate();
				return null;
			}
		});
	}
}
